package com.aircms.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class Error implements ErrorController {

    private static final Logger logger=LoggerFactory.getLogger(Error.class);

    @Override
    public String getErrorPath() {
        return null;
    }

    /**
     * 拦截控制器请求错误信息
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    public ServerResponse defaultErrorHandler(HttpServletRequest req, Exception e, HttpServletResponse response) throws Exception {
        logger.error(String.valueOf("path:"+req.getContextPath())+String.valueOf(req.getServletPath())+
                String.valueOf(req.getQueryString()));
        logger.error(e.getMessage());
        return ServerResponse.resResponse("",-500,e.getMessage());
    }

}
